package com.ruoyi.business.uitls;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;


public class SmsUtil {
    //替换成自己申请的accessKeyId
    private static String accessKeyId = "LTAI5tNEK8v9tnBzhBzmEMiK";
    //替换成自己申请的accessKeySecret
    private static String accessKeySecret = "zq4SyufAUUsS8xwd1hYPK5oQgXdJou";
    static final String product = "Dysmsapi";
    static final String domain = "dysmsapi.aliyuncs.com";

    /**
     * @param phoneNumbers 要发送短信到哪个手机号
     * @param signName     短信签名[必须使用前面申请的]
     * @param templateCode 短信短信模板ID[必须使用前面申请的]
     * @param param        模板中${code}位置传递的内容
     */
    public static void sendSms(String phoneNumbers, String signName, String
            templateCode, String param) {
        try {
            System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
            IClientProfile profile = DefaultProfile.getProfile("cn-beijing",
                    accessKeyId, accessKeySecret);
            DefaultProfile.addEndpoint("cn-beijing", "cn-beijing", product,
                    domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumbers(phoneNumbers);
            request.setSignName(signName);
            request.setTemplateCode(templateCode);
            request.setTemplateParam(param);
            request.setOutId("yourOutId");

            Config config = new Config()
                    // 您的AccessKey ID
                    .setAccessKeyId(accessKeyId)
                    // 您的AccessKey Secret
                    .setAccessKeySecret(accessKeySecret);
// 访问的域名
            config.endpoint = "dysmsapi.aliyuncs.com";
            Client client = new Client(config);
            SendSmsResponse response = client.sendSms(request);
//            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
//            if (!"OK".equals(sendSmsResponse.getCode())) {
//                throw new RuntimeException(sendSmsResponse.getMessage());
//            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("发送短信失败");
        }
    }
}